#import "StepBarController.h"

@interface StepBarController ()

@property (nonatomic, strong) NSArray *progressStatus; // 步骤状态数据
@property (nonatomic, strong) NSMutableArray *stepItems; // 步骤项容器，用于动态更新

@end

@implementation StepBarController

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor whiteColor];
//        self.layer.cornerRadius = 8.0f;
//        self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
//        self.layer.shadowOpacity = 0.5f;
//        self.layer.shadowRadius = 3.0f;
//        self.layer.shadowOffset = CGSizeMake(0, 2);
        self.stepItems = [NSMutableArray array];
    }
    return self;
}

// 从外部设置步骤数据
- (void)setProgressStatus:(NSArray *)statusArray currentStep:(NSInteger)stepIndex {
    self.progressStatus = statusArray;
    
    [self setupStepBar];
    
    self.currentStepIndex = stepIndex;
   
}

// 设置步骤条
- (void)setupStepBar {
    // 清空现有步骤项
    for (UIView *item in self.stepItems) {
        [item removeFromSuperview];
    }
    [self.stepItems removeAllObjects];
    
    if (self.progressStatus.count == 0) return;
    
    CGFloat itemWidth = 36.0f;
    CGFloat itemHeight = 25.0f;
    CGFloat dotSize = 8.0f;
    CGFloat spacing = 43.0f; // 步骤间间距
    CGFloat totalWidth = (itemWidth * self.progressStatus.count) + (spacing * (self.progressStatus.count - 1));
    CGFloat startX = (self.bounds.size.width - totalWidth) / 2.0f;
    
    for (NSInteger i = 0; i < self.progressStatus.count; i++) {
        NSString *title = self.progressStatus[i];
        CGFloat itemX = startX + (itemWidth + spacing) * i;
        
        // 创建步骤项视图
        UIView *itemView = [[UIView alloc] initWithFrame:CGRectMake(itemX, 0, itemWidth, itemHeight)];
        itemView.tag = i;
        [self addSubview:itemView];
        [self.stepItems addObject:itemView];
        
        
        
        // 创建状态圆点
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake((itemWidth - dotSize) / 2.0f, 5.0f, dotSize, dotSize)];
        dotView.layer.cornerRadius = dotSize / 2.0f;
        dotView.tag = i + 100;
        [itemView addSubview:dotView];
        
        // 创建外围圆圈（用于当前步骤）
        UIView *outerCircle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, dotSize + 4.0f, dotSize + 4.0f)];
        outerCircle.layer.cornerRadius = (dotSize + 4.0f) / 2.0f;
        outerCircle.layer.borderWidth = 1.f;
        outerCircle.layer.borderColor = [UIColor clearColor].CGColor;
        outerCircle.backgroundColor = [UIColor clearColor];
        outerCircle.tag = i + 200;
        [itemView addSubview:outerCircle];
        [itemView sendSubviewToBack:outerCircle];
        [outerCircle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(dotView);
            make.width.height.offset(dotSize + 4.0f);
        }];
        
        // 创建标题标签
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2 + dotSize + 7, itemWidth, 20.0f)];
        titleLabel.text = title;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:8.0f];
        titleLabel.textColor = hexColor(373D41);
        titleLabel.tag = i + 300;
        [itemView addSubview:titleLabel];
        
        
        // 创建连接线（除最后一个步骤）
        if (i < self.progressStatus.count - 1) {
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(itemX + itemWidth, dotView.center.y, spacing, 1.0f)];
            lineView.backgroundColor = hexColorAlpha(226CFE, 0.1);
            [self addSubview:lineView];
        }
        
        // 更新步骤状态
        [self updateStepStatusWithIndex:i];
    }
}

// 更新步骤状态
- (void)updateStepStatusWithIndex:(NSInteger)index {
    for (UIView *itemView in self.stepItems) {
        NSInteger itemIndex = itemView.tag;
        UIView *dotView = [itemView viewWithTag:itemIndex + 100];
        UIView *outerCircle = [itemView viewWithTag:itemIndex + 200];
        UILabel *titleLabel = [itemView viewWithTag:itemIndex + 300];
        
        if (dotView && titleLabel) {
            if (itemIndex < self.currentStepIndex) {
                // 已完成状态
                dotView.backgroundColor = main_color;
                outerCircle.layer.borderColor = [UIColor clearColor].CGColor;
            } else if (itemIndex == self.currentStepIndex) {
                // 当前步骤状态
                dotView.backgroundColor = main_color;
                outerCircle.layer.borderColor = main_color.CGColor;
            } else {
                // 未开始状态
                dotView.backgroundColor = hexColorAlpha(226CFE, 0.1);
                outerCircle.layer.borderColor = [UIColor clearColor].CGColor;
            }
        }
    }
}

// 动态更新当前步骤
- (void)setCurrentStepIndex:(NSInteger)currentStepIndex {
    _currentStepIndex = currentStepIndex;
    [self updateStepStatusWithIndex:currentStepIndex];
}

@end
